/*
 * base.js
 * 基础api配置
 *
 */
import axios from 'axios';

const config = {
  baseURL: process.env.VUE_APP_BASE_API,
  timeout: 0,
};

const instance = axios.create(config);
instance.defaults.headers.common['Content-Type'] = 'application/json;charset=UTF-8';

// 添加请求拦截器
instance.interceptors.request.use(
  config => {
    const configs = config;
    // if (sessionStorage.getItem('token')) {
    //   configs.data = {
    //     accessToken: sessionStorage.getItem('token'),
    //     param: configs.data,
    //   };
    // }
    return configs;
  },
  error => Promise.reject(error),
);

// 添加响应拦截器
instance.interceptors.response.use(
  response => {
    let res;
    const { request } = response;
    if (request.responseType === 'blob') {
      res = response;
    } else {
      res = response.data;
    }
    return res;
  },
  error => {
    return Promise.reject(error);
  },
);

export default instance;
